package com.lemon.smartsafetynew.ui.activity.ledger;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.ApplicationInfo;
import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.data.app.DataUtil;
import com.lemon.smartsafety.entity.safe.company.CompanyInfo;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafety.ui.activity.safe.data.CompanyBasicInfoActivity;
import com.lemon.smartsafety.ui.activity.safe.data.contract.KeepContractListActivity;
import com.lemon.smartsafety.ui.activity.safe.data.safe_manager.SafeManagerListActivity;
import com.lemon.smartsafety.ui.activity.safe.data.wuye.WuyeRecordListActivity;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.lemon.smartsafetynew.entity.User;
import com.lemon.smartsafetynew.entity.ledger.LedgerPanelButton;
import com.lemon.smartsafetynew.ui.adapter.ledger.LedgerHomeAdapter;
import com.lemon.smartsafetynew.ui.fragment.ledger.LedgerBuildingBannerFragment;
import com.lemon.smartsafetynew.util.user.AccountUtil;
import com.tmall.ultraviewpager.UltraViewPager;

import java.util.List;

import butterknife.Bind;

/**
 * Created by Andrew on 2017/8/14.
 */

public class LedgerHomeActivity extends SwipeBackActivity implements RecyclerTClickListener<LedgerPanelButton> {

    @Bind(R.id.ultra_viewpager)
    UltraViewPager ultraViewPager;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.label_1_tv)
    TextView label1Tv;
    @Bind(R.id.label_2_tv)
    TextView label2Tv;

    private CompanyInfo mCompanyInfo = null;

    private LedgerHomeAdapter ledgerHomeAdapter;

    private User user;

    @Override
    protected void initTitle() {
        this.setBarTitle("安全台账");
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(LedgerHomeActivity.this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_ledger_home;
    }

    @Override
    protected void initViews() {
        super.initViews();

        initData();


        // ============== ultraViewPager ===============
        ultraViewPager.setScrollMode(UltraViewPager.ScrollMode.HORIZONTAL);
        //initialize UltraPagerAdapter，and add child view to UltraViewPager
        PagerAdapter adapter = new LedgerViewpagerAdapter(getSupportFragmentManager());
        ultraViewPager.setAdapter(adapter);


        // 现在只有一页数据
//        //initialize built-in indicator
//        ultraViewPager.initIndicator();
//        //set style of indicators
//        ultraViewPager.getIndicator()
//                .setOrientation(UltraViewPager.Orientation.HORIZONTAL)
//                .setFocusColor(0xFFCDCB46)
//                .setNormalColor(0xFFAAECD9)
//                .setRadius((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics()));
//        //set the alignment
//        ultraViewPager.getIndicator().setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
//        ultraViewPager.getIndicator().setMargin(0, 0, 0, 15);
//        //construct built-in indicator, and add it to  UltraViewPager
//        ultraViewPager.getIndicator().build();

//        //set an infinite loop
//        ultraViewPager.setInfiniteLoop(false);
//        //enable auto-scroll mode
//        ultraViewPager.setAutoScroll(5000);


        // ============== recycler ===============
        List<LedgerPanelButton> mDataList = DataUtil.getLedgerPanel();

        GridLayoutManager layoutManager = new GridLayoutManager(IApplication.context(), 3);
        ledgerHomeAdapter = new LedgerHomeAdapter(mDataList, this);
        recyclerView.setAdapter(ledgerHomeAdapter);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setNestedScrollingEnabled(false);


        // ============ Title UI ==============
        if(user != null) {
            label1Tv.setText(user.getCompanyName());
            label2Tv.setText(ApplicationInfo.userTypeMap.get(user.getUserType()));
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
        ultraViewPager.setAutoScroll(5000);
    }

    @Override
    protected void onPause() {
        super.onPause();
        ultraViewPager.disableAutoScroll();
    }

    private void initData() {

        // ============= CompanyInfo =============
        user = AccountUtil.getUser();
        if (user != null) {
            mCompanyInfo = new CompanyInfo();
            mCompanyInfo.setCompany_code(StringUtils.nullValue(user.getCompanyCode()));
            mCompanyInfo.setCompany_name(StringUtils.nullValue(user.getCompanyName()));
        }

    }

    @Override
    public void onItemClick(LedgerPanelButton mLedgerPanelButton, int position) {

        if (mLedgerPanelButton.getLedgerOptionCodeEnum() == ApplicationInfo.LedgerOptionCodeEnum.COMMON_INFO) {
            //基本信息
            Bundle bundle = new Bundle();
            bundle.putSerializable(BundleKey.COMPANY_INFO, mCompanyInfo);
            UIHelper.showActivity(LedgerHomeActivity.this, bundle, CompanyBasicInfoActivity.class);

        } else if (mLedgerPanelButton.getLedgerOptionCodeEnum() == ApplicationInfo.LedgerOptionCodeEnum.BUILDING_INFO) {
            //建筑信息
            Bundle bundle = new Bundle();
            bundle.putSerializable(BundleKey.COMPANY_INFO, mCompanyInfo);
            UIHelper.showSafeDataCollectionBuildingList(LedgerHomeActivity.this, bundle);
        } else if (mLedgerPanelButton.getLedgerOptionCodeEnum() == ApplicationInfo.LedgerOptionCodeEnum.EMERGENCY) {

            //应急处置
            UIHelper.showActivity(LedgerHomeActivity.this, LedgerEmergencyActivity.class);

        } else if (mLedgerPanelButton.getLedgerOptionCodeEnum() == ApplicationInfo.LedgerOptionCodeEnum.MAINTENANCE) {
            //维保登记
            Bundle bundle = new Bundle();
            bundle.putSerializable(BundleKey.COMPANY_INFO, mCompanyInfo);
            UIHelper.showActivity(LedgerHomeActivity.this, bundle, KeepContractListActivity.class);
        } else if (mLedgerPanelButton.getLedgerOptionCodeEnum() == ApplicationInfo.LedgerOptionCodeEnum.PROPERTY) {
            //物业登记
            Bundle bundle = new Bundle();
            bundle.putSerializable(BundleKey.COMPANY_INFO, mCompanyInfo);
            UIHelper.showActivity(LedgerHomeActivity.this, bundle, WuyeRecordListActivity.class);
        } else if (mLedgerPanelButton.getLedgerOptionCodeEnum() == ApplicationInfo.LedgerOptionCodeEnum.SAFE_SETTING) {
            //安全设置
            Bundle bundle = new Bundle();
            bundle.putSerializable(BundleKey.COMPANY_INFO, mCompanyInfo);
            UIHelper.showActivity(LedgerHomeActivity.this, bundle, SafeManagerListActivity.class);
        }

    }

    private class LedgerViewpagerAdapter extends FragmentStatePagerAdapter {

        public LedgerViewpagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
//                case 0:
//                    return LedgerRadarFragment.newInstance();
//                case 1:
//                    return LedgerPatrolBannerFragment.newInstance(position);
//                case 2:
//                    return LedgerMonitorBannerFragment.newInstance();
                case 0:
                    return LedgerBuildingBannerFragment.newInstance();
                default:
                    return LedgerBuildingBannerFragment.newInstance();
            }
        }

        @Override
        public int getCount() {
            return 1;
        }

    }


}
